package com.rules.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rules.admin.dao.ValidatorModelDao;
import com.rules.admin.entity.ValidatorModel;
import com.rules.admin.service.ValidatorService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ValidatorServiceImpl extends ServiceImpl<ValidatorModelDao, ValidatorModel> implements ValidatorService {
   public Map<String,Map<String,Object>>  findByProjectId(Long projectId) {
      Map<String,Map<String,Object>>  map = new HashMap<>();
//   ValidatorModelExample example = new ValidatorModelExample();
//   Criteria criteria = example.createCriteria();
//   criteria.andProjectIdEqualTo(projectId);

      LambdaQueryWrapper<ValidatorModel> queryWrapper = new LambdaQueryWrapper<>();
      List<ValidatorModel> validatorModelList = this.baseMapper.selectList(queryWrapper);
      Iterator iterator = validatorModelList.iterator();

      while(iterator.hasNext()) {
         ValidatorModel validatorModel = (ValidatorModel)iterator.next();
         map.put(validatorModel.getValidatorName(), (Map<String,Object>)JSONObject.parse(validatorModel.getValidator()));
      }
      return map;
   }

   public IPage<ValidatorModel> queryPage(ValidatorModel validatorModel, IPage<ValidatorModel> page) {
//      ValidatorModelExample example = new ValidatorModelExample();
//      Criteria criteria = example.createCriteria();
//      example.setPage(page);

      LambdaQueryWrapper<ValidatorModel> queryWrapper = new LambdaQueryWrapper<>();

      if (validatorModel.getValidatorName() != null && !"".equals(validatorModel.getValidatorName())) {
         queryWrapper.like(ValidatorModel::getValidatorName,validatorModel.getValidatorName());
      }
      queryWrapper.ge(ValidatorModel::getProjectId,validatorModel.getProjectId());
      return this.baseMapper.selectPage(page,queryWrapper);
   }
}
